package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MakeFriendManager;
import com.itheima.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MakeFriendController {

    @Autowired
    private MakeFriendManager makeFriendManager;


    //获取环信用户信息
    @GetMapping("/huanxin/user")
    public Map<String, String> huanxinUser() {
        Long userId = UserHolder.get().getId();

        Map<String, String> map = new HashMap<>();
        map.put("username", userId.toString());
        map.put("password", "123456");

        return map;
    }

    //今日佳人
    @GetMapping("/tanhua/todayBest")
    public RecommendUserVo findTodayBest(){
        return makeFriendManager.findTodayBest();
    }


    //推荐用户列表
    @GetMapping("/tanhua/recommendation")
    public PageBeanVo findRecommendUser(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ) {
        return makeFriendManager.findRecommendUser(pageNum, pageSize);
    }


    //佳人详情
    @GetMapping("/tanhua/{id}/personalInfo")
    public RecommendUserVo findPersonalInfo(@PathVariable("id") Long recommendUserId) {

        return makeFriendManager.findPersonalInfo(recommendUserId);
    }


    //查询陌生人问题(聊一下)
    @GetMapping("/tanhua/strangerQuestions")
    public String findStrangerQuestion(Long userId){
        return makeFriendManager.findStrangerQuestion(userId);
    }


    //谁看过我
    @GetMapping("/movements/visitors")
    public List<VisitorVo> findVisitor(){
        return makeFriendManager.findVisitor();
    }


    //上报用户地理位置
    @PostMapping("/baidu/location")
    public void saveUserLocation(@RequestBody Map<String, String> map) {
        //1. 接收参数
        Double longitude = Double.parseDouble(map.get("longitude"));
        Double latitude = Double.parseDouble(map.get("latitude"));
        String address = map.get("addrStr");

        System.out.println("用户开始上报位置" + map);

        //2. 调用manager保存
        makeFriendManager.saveUserLocation(longitude, latitude, address);
    }


    //附近的人
    @GetMapping("/tanhua/search")
    public List<NearUserVo> findNearUserList(String gender, Long distance){

        return makeFriendManager.findNearUserList(gender,distance);
    }


    //回复陌生人问题(给佳人打招呼)
    @PostMapping("/tanhua/strangerQuestions")
    public void replyStrangerQuestion(@RequestBody Map<String, String> map) {
        String strangerId = map.get("userId");
        String reply = map.get("reply");

        makeFriendManager.replyStrangerQuestion(strangerId, reply);
    }


    //添加联系人
    @PostMapping("/messages/contacts")
    public void saveContact(@RequestBody Map<String, String> map) {

        Long friendId = Long.parseLong(map.get("userId"));

        makeFriendManager.saveContact(friendId);

    }


    //查询联系人
    @GetMapping("/messages/contacts")
    public PageBeanVo findContact(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ) {

        return makeFriendManager.findContact(pageNum, pageSize);

    }


    //给推荐卡片提供20条分页信息
    @GetMapping("/tanhua/cards")
    public List<UserLikeVo> findTanhuaCards() {
        return (List<UserLikeVo>) makeFriendManager.findRecommendUser(1, 20).getItems();
    }
}










